<?php

namespace app\controllers\admin;

use app\controllers\admin\base\AdminController;
use app\models\admin_ops\AdminOpsRefill as Model;

class RefillController extends AdminController
{
    public function actionList()
    {
        $user_id = $this->post('user_id');

        $export = $this->post('export');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $query = Model::find();
        $query->orderBy(['id' => SORT_DESC]);

        if ($user_id) {
            $query->andWhere(['user_id' => $user_id]);
        }

        // export
        if ($export) {
            return $this->success($query->asArray()->all());
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

}